<?php
namespace App\Http\Controllers\Search;
use App\Http\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Models\DyModels\KeywordVideo;
class KeywordVideoAjaxController extends ApiBaseController {
	public function __construct(Request $request) {
		parent::__construct($request);
	}
  
	/**
	* 关键词视频列表
	*/
	public function dataList(){
		$oKeywordVideo = new KeywordVideo();
		$result = $oKeywordVideo->getPageData($this->params);
		echo json_encode($result);
	}
	
	
	//搜索关键词列表
	public function searchkeyword(){
		$data=\App\Components\Tools::getThird('/config/keywords.json');
		$kname=isset($this->params['kname']) ? $this->params['kname'] : '';
		$html="";$i=0;
		if(isset($data['keywords'])){
			foreach($data['keywords'] as $k=>$v){
			    if($kname && strstr($v,$kname)!==false){
			        $html.="<tr>";
						$html.="<td>".++$i."</td>";
						$html.="<td>$v</td>";
						$html.="<td>";
							$html.='<a href="javascript:;" onclick="updateModal('."'$k'".','."'$v'".')" title="修改"><span class="glyphicon glyphicon-pencil"></span></a>';
							$html.="&nbsp;&nbsp;&nbsp;";
							$html.='<a href="javascript:void(0);" title="删除" onclick="deletModel('."'$k'".')"><span class="glyphicon glyphicon-trash"></span></a>';
						$html.="</td>";
					$html.="</tr>";
			    }elseif(!$kname){
			        $html.="<tr>";
						$html.="<td>".++$i."</td>";
						$html.="<td>$v</td>";
						$html.="<td>";
							$html.='<a href="javascript:;" onclick="updateModal('."'$k'".','."'$v'".')" title="修改"><span class="glyphicon glyphicon-pencil"></span></a>';
							$html.="&nbsp;&nbsp;&nbsp;";
							$html.='<a href="javascript:void(0);" title="删除" onclick="deletModel('."'$k'".')"><span class="glyphicon glyphicon-trash"></span></a>';
						$html.="</td>";
					$html.="</tr>";
			    }
			}
		}
		$html ? $html : ($html.="<tr><td colspan='3'>无数据</td></tr>");
		$this->returnJson(0,$html);
	}
	
	//添加修改关键词
	public function saveKeyword(){
		$data=\App\Components\Tools::getThird('/config/keywords.json');
		$kid=isset($this->params['kid']) && $this->params['kid']!='' ? $this->params['kid'] : -1;
		$kname=isset($this->params['kname']) ? $this->params['kname'] : '';
		$ktitle=$kid>=0 ? '修改成功' : '添加成功';
		if(!$kname){$this->returnJson(1,'请输入关键词');}
		
		if($kid>=0){
			$data['keywords'][$kid]=$kname;
		}else{
			$data['keywords'][]=$kname;
		}
		
		\App\Components\Tools::setThird('/config/keywords.json',json_encode($data));
		$this->returnJson(0,$ktitle);
	}
	//删除关键词
	public function deletKeyword(){
		$data=\App\Components\Tools::getThird('/config/keywords.json');
		$kid=isset($this->params['kid']) ? $this->params['kid'] : '';
		if($kid!=='' && $kid>=0){
			unset($data['keywords'][$kid]);
			$data['keywords']=array_values($data['keywords']);
			\App\Components\Tools::setThird('/config/keywords.json',json_encode($data));
			$this->returnJson(0,"删除成功");
		}
		$this->returnJson(1,"删除失败");
	}
}
